<script setup>
import { ref } from 'vue'
import { Edit, Delete } from '@element-plus/icons-vue'
import ChannelSelect from './component/ChannelSelect.vue'
import {
  articleGetArticleListService,
  articleDeleteArticleService
} from '@/api/article'
import { formatDate } from '@/utils/format'
import ArticleDrawer from './component/ArticleDrawer.vue'
import { ElMessage, ElMessageBox } from 'element-plus'

// 文章列表
const articleList = ref([])
const total = ref(0) // 文章总数
const loading = ref(false) // 加载状态

// 请求参数
const queryParams = ref({
  pagenum: 1,
  pagesize: 5,
  cate_id: '',
  state: ''
})

// 请求文章列表
const getArticleList = async () => {
  loading.value = true
  const res = await articleGetArticleListService(queryParams.value)
  articleList.value = res.data.data
  total.value = res.data.total
  loading.value = false
}
getArticleList()

// size改变时请求文章列表
const handleSizeChange = (size) => {
  queryParams.value.pagesize = size
  queryParams.value.pagenum = 1
  getArticleList()
}
// current改变时请求文章列表
const handleCurrentChange = (page) => {
  queryParams.value.pagenum = page
  getArticleList()
}

const articleDrawerRef = ref(null) // 编辑文章详情的弹窗
// 添加文章
const handleAdd = () => {
  articleDrawerRef.value.openDrawer({})
}
// 编辑文章
const handleEdit = (row) => {
  // console.log(row)
  articleDrawerRef.value.openDrawer(row)
}
// 删除文章
const handleDelete = async (row) => {
  // console.log(row)
  await ElMessageBox.confirm('你确认删除该文章信息吗？', '温馨提示', {
    type: 'warning',
    confirmButtonText: '确认',
    cancelButtonText: '取消'
  })
  await articleDeleteArticleService(row.id)
  ElMessage.success('删除成功')
  getArticleList()
}

// 搜索
const onSearch = () => {
  queryParams.value.pagenum = 1
  getArticleList()
}
// 重置
const onReset = () => {
  queryParams.value = {
    pagenum: 1,
    pagesize: 5,
    cate_id: '',
    state: ''
  }
  getArticleList()
}
// 文章发布成功回调
const onSuccess = (type) => {
  if (type === 'add') {
    queryParams.value.pagenum = Math.ceil(
      (total.value + 1) / queryParams.value.pagesize
    )
  }
  getArticleList()
}
</script>
<template>
  <page-container title="文章管理">
    <template #extra>
      <el-button @click="handleAdd" type="primary">发布文章</el-button>
    </template>
    <!--form表单区域-->
    <el-form inline>
      <el-form-item label="文章分类：">
        <!--Vue2中的v-model是 :value和@input的简写-->
        <!--Vue3中的v-model是 :modelValue和@update:modelValue的简写-->
        <!--相当于Vue3中结合了sync修饰符和v-model，形成这种统一的写法
        默认用modelValue可以省略:modelValue，但也可以用完整写法更改成其他的名字
        相较于Vue2来说更灵活，更方便使用-->
        <channel-select v-model="queryParams.cate_id"></channel-select>
      </el-form-item>
      <el-form-item label="发布状态：">
        <el-select style="width: 200px" v-model="queryParams.state">
          <el-option label="已发布" value="已发布"></el-option>
          <el-option label="草稿" value="草稿"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSearch">搜索</el-button>
        <el-button style="margin-left: 10px" @click="onReset">重置</el-button>
      </el-form-item>
    </el-form>

    <!--table表格区域-->
    <el-table :data="articleList" v-loading="loading">
      <el-table-column prop="title" label="文章标题" width="400">
        <template #default="{ row }">
          <el-link type="primary" :underline="false">{{ row.title }}</el-link>
        </template>
      </el-table-column>
      <el-table-column prop="cate_name" label="分类"></el-table-column>
      <el-table-column prop="pub_date" label="发表时间">
        <template #default="{ row }">
          {{ formatDate(row.pub_date) }}
        </template>
      </el-table-column>
      <el-table-column prop="state" label="状态"></el-table-column>
      <el-table-column label="操作">
        <template #default="{ row }">
          <el-button
            circle
            plain
            type="primary"
            :icon="Edit"
            size="small"
            @click="handleEdit(row)"
          ></el-button>
          <el-button
            circle
            plain
            type="danger"
            :icon="Delete"
            size="small"
            @click="handleDelete(row)"
          ></el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="暂无数据" />
      </template>
    </el-table>

    <!--分页区域-->
    <el-pagination
      style="margin-top: 20px; justify-content: flex-end"
      v-model:current-page="queryParams.pagenum"
      v-model:page-size="queryParams.pagesize"
      :page-sizes="[2, 3, 5, 10]"
      layout="jumper,total, sizes, prev, pager, next"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />

    <!--文章编辑弹窗-->
    <article-drawer
      ref="articleDrawerRef"
      @success="onSuccess"
    ></article-drawer>
  </page-container>
</template>
